using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OrderingSystem.Core.Entities;
using OrderingSystem.Infrastructure.Data;

namespace OrderingSystem.API.Controllers
{
    [ApiController]
    [Route("caipinpingjia")]
    public class DishReviewController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public DishReviewController(ApplicationDbContext context)
        {
            _context = context;
        }

        [HttpGet("page")]
        [AllowAnonymous]
        public async Task<IActionResult> GetReviewPage(
            [FromQuery] int page = 1,
            [FromQuery] int limit = 10,
            [FromQuery] string sort = "addtime",
            [FromQuery] string order = "desc")
        {
            try
            {
                var query = _context.Reviews.AsQueryable();

                // 排序
                if (sort == "addtime")
                {
                    query = order == "desc"
                        ? query.OrderByDescending(r => r.addtime)
                        : query.OrderBy(r => r.addtime);
                }
                else if (sort == "id")
                {
                    query = order == "desc"
                        ? query.OrderByDescending(r => r.id)
                        : query.OrderBy(r => r.id);
                }

                // 获取总记录数
                var total = await query.CountAsync();

                // 获取分页数据
                var reviews = await query
                    .Skip((page - 1) * limit)
                    .Take(limit)
                    .Select(r => new
                    {
                        id = r.id,
                        addtime = r.addtime,
                        dingdanbianhao = r.dingdanbianhao,
                        caipinmingcheng = r.caipinmingcheng,
                        caipinfenlei = r.caipinfenlei,
                        pingfen = r.pingfen,
                        tianjiatupian = r.tianjiatupian,
                        pingjianeirong = r.pingjianeirong,
                        pingjiariqi = r.pingjiariqi,
                        yonghuming = r.yonghuming,
                        lianxidianhua = r.lianxidianhua,
                        sfsh = r.sfsh,
                        shhf = r.huifuneirong,
                        userid = r.userid
                    })
                    .ToListAsync();

                return Ok(new
                {
                    code = 0,
                    data = new
                    {
                        total,
                        pageSize = limit,
                        totalPage = (int)Math.Ceiling((double)total / limit),
                        currPage = page,
                        list = reviews
                    }
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取菜品评价分页数据时发生错误: {ex.Message}");
                Console.WriteLine($"异常堆栈: {ex.StackTrace}");
                return StatusCode(500, new { code = 500, msg = "服务器内部错误", error = ex.Message });
            }
        }

        [HttpGet("list")]
        [AllowAnonymous]
        public async Task<IActionResult> GetReviews()
        {
            try
            {
                var reviews = await _context.Reviews
                    .OrderByDescending(r => r.addtime)
                    .Select(r => new
                    {
                        id = r.id,
                        addtime = r.addtime,
                        dingdanbianhao = r.dingdanbianhao,
                        caipinmingcheng = r.caipinmingcheng,
                        caipinfenlei = r.caipinfenlei,
                        pingfen = r.pingfen,
                        tianjiatupian = r.tianjiatupian,
                        pingjianeirong = r.pingjianeirong,
                        pingjiariqi = r.pingjiariqi,
                        yonghuming = r.yonghuming,
                        lianxidianhua = r.lianxidianhua,
                        sfsh = r.sfsh,
                        shhf = r.huifuneirong,
                        userid = r.userid
                    })
                    .ToListAsync();

                return Ok(new { code = 0, data = reviews });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取菜品评价列表时发生错误: {ex.Message}");
                Console.WriteLine($"异常堆栈: {ex.StackTrace}");
                return StatusCode(500, new { code = 500, msg = "服务器内部错误", error = ex.Message });
            }
        }

        [HttpGet("detail/{id}")]
        [AllowAnonymous]
        public async Task<IActionResult> GetReviewDetail(long id)
        {
            try
            {
                var review = await _context.Reviews
                    .Select(r => new
                    {
                        id = r.id,
                        addtime = r.addtime,
                        dingdanbianhao = r.dingdanbianhao,
                        caipinmingcheng = r.caipinmingcheng,
                        caipinfenlei = r.caipinfenlei,
                        pingfen = r.pingfen,
                        tianjiatupian = r.tianjiatupian,
                        pingjianeirong = r.pingjianeirong,
                        pingjiariqi = r.pingjiariqi,
                        yonghuming = r.yonghuming,
                        lianxidianhua = r.lianxidianhua,
                        sfsh = r.sfsh,
                        shhf = r.huifuneirong,
                        userid = r.userid
                    })
                    .FirstOrDefaultAsync(r => r.id == id);

                if (review == null)
                {
                    return NotFound(new { code = 404, msg = "评价不存在" });
                }

                return Ok(new { code = 0, data = review });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取菜品评价详情时发生错误: {ex.Message}");
                Console.WriteLine($"异常堆栈: {ex.StackTrace}");
                return StatusCode(500, new { code = 500, msg = "服务器内部错误", error = ex.Message });
            }
        }

        [HttpGet("info/{id}")]
        [AllowAnonymous]
        public async Task<IActionResult> GetReviewInfo(long id)
        {
            return await GetReviewDetail(id);
        }
    }
} 